﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GiMessCommons;

namespace GiMessServer.Net
{
    public class NetMsgHandler
    {
        private DataFieldsReader dataReader;
        private DataFieldsWriter dataWriter;
        
        public void HandleMsg(NetClient sender, NetMsg msg)
        {
            Loger.LogLine("Message", Loger.ServerLogLevel.Verbose);
            Loger.LogLine(((NetServices)msg.Service).ToString(), Loger.ServerLogLevel.Verbose);
            Loger.LogLine(msg.Data, Loger.ServerLogLevel.Verbose);
            switch ((NetServices)msg.Service)
            {
                case NetServices.Auth:
                    UserAuth(sender, msg);
                    break;
                case NetServices.ListRequest:
                    SendList(sender, msg);
                    break;
                case NetServices.SendMessage:
                    GiMessServer.Instance.UserIM(msg.Data);
                    break;
            }
        }

        private void UserAuth(NetClient sender, NetMsg msg)
        {
            string userName;
            string userPass;

            dataReader = new DataFieldsReader(msg.Data);
            userName   = dataReader.GetFieldData(Field.AuthUserName);
            userPass   = dataReader.GetFieldData(Field.AuthUserPassword);

            //GiMessServer.Instance.UserLogin(sender, userName, userPass);

            if (CheckCredidentials(userName, userPass))
            {
                dataWriter = new DataFieldsWriter();
                dataWriter.WriteField(Field.AuthUserFristName, Database.GetFirstName(userName));
                dataWriter.WriteField(Field.AuthUserLastName,  Database.GetLastName (userName));

                sender.SendNetMsg(NetServices.LoginAccepted, dataWriter.GetString());

                sender.Credidentials = new AuthInfo(userName);

                Database.SetOnlineStatus(userName, true);
                NotifyFriends(userName);
                //set sender AuthInfo
                //GiMessServer.Instance.AcceptLogin(sender);
            }
            else
            {
                sender.SendNetMsg(NetServices.LoginFailed, "");
            }
        }

        private void NotifyFriends(string userName)
        {
            UserFriend[] friends = Database.GetUserList(userName);

            dataWriter = new DataFieldsWriter();
            dataWriter.WriteField(Field.UpdateUserName, userName);
            dataWriter.WriteField(Field.UpdateOnline, "true");

            NetMsg msg = new NetMsg(NetServices.UpdateOnline, dataWriter.GetString());
            
            for (int i = 0; i < friends.Length; i++)
            {
                if (friends[i].Online)
                    GiMessServer.Instance.Notify(friends[i].Username, msg);
            }
        }

        private void SendList(NetClient sender, NetMsg msg)
        {
            UserFriend[] friends = Database.GetUserList(sender.Credidentials.UserName);

            dataWriter = new DataFieldsWriter();
            dataWriter.WriteField(Field.ListCount, friends.Length.ToString());

            for(int i = 0; i<friends.Length; i++)
            {
                dataWriter.WriteField(Field.ListUserIndex,     i.ToString());
                dataWriter.WriteField(Field.ListUserGroup,     friends[i].Group);
                dataWriter.WriteField(Field.ListUserName,      friends[i].Username);
                dataWriter.WriteField(Field.ListUserFirstName, friends[i].FirstName);
                dataWriter.WriteField(Field.ListUserLastName,  friends[i].LastName);
                dataWriter.WriteField(Field.ListUserOnline,    friends[i].Online.ToString());
                dataWriter.WriteField(Field.ListUserStatus,    friends[i].Status);
            }

            sender.SendNetMsg(NetServices.ListSend, dataWriter.GetString());
        }

        private bool CheckCredidentials(string userName, string userPassword)
        {
            string dbPassword;
            dbPassword = Database.GetUserPassword(userName);

            return dbPassword.Equals(userPassword, StringComparison.Ordinal);
        }
    }
}
